package elion.com.theElion.fragmentLandData;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import elion.com.elion.R;
import elion.com.theElion.util.adapter.DesertSelectFragmentAdapter;
import elion.vo.Desert;

/**
 * A fragment representing a list of Items.
 * <p/>
 * interface.
 */
public class DesertSelectFragment extends Fragment
{

    private RecyclerView recyclerView;
    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";

    List<Desert> deserts;
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private DesertSelectFragmentAdapter.OnItemClickLitener mItemClickListener;
    private View view;
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public DesertSelectFragment()
    {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static DesertSelectFragment newInstance(int columnCount)
    {
        DesertSelectFragment fragment = new DesertSelectFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if (getArguments() != null)
        {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_desert_select_item_list, container, false);
        initList();
        // Set the adapter
        if (view instanceof LinearLayout)
        {
            Context context = view.getContext();
            recyclerView = (RecyclerView) view.findViewById(R.id.list_desert_select);

            if (mColumnCount <= 1)
            {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                mItemClickListener = new DesertSelectFragmentAdapter.OnItemClickLitener()
                {

                    @Override
                    public void onItemClick(View view, int position)
                    {
                        LandSelectFragment landSelectFragment = LandSelectFragment.newInstance(1,view.getTag()+"");
                        FragmentManager fragmentManager =getActivity().getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
//                        fragmentTransaction.replace(R.id.fragment_container,landSelectFragment);
                        fragmentTransaction.replace(R.id.fg_container,landSelectFragment);

//                        fragmentTransaction.setCustomAnimations(R.animator.fragment_slide_left_enter,R.animator.fragment_slide_left_exit);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();
                    }

                    @Override
                    public void onItemLongClick(View view, int position)
                    {

                    }
                };
                recyclerView.setAdapter(new DesertSelectFragmentAdapter(deserts, mItemClickListener));



            }
            else
            {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
        }
        return view;
    }



    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
//        if (context instanceof OnListFragmentInteractionListener)
//        {
//            mListener = (OnListFragmentInteractionListener) context;
//        }
//        else
//        {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnListFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
    }
    //如果沙漠有增加，从这里更改
    private void initList()
    {
        deserts= new ArrayList<>();
        deserts.add(new Desert("库布其沙漠",4,null));
        deserts.add(new Desert("乌兰布和沙漠",5,null));
        deserts.add(new Desert("毛乌素沙地",7,null));
        deserts.add(new Desert("西藏那曲",8,null));
        deserts.add(new Desert("腾格里沙漠",9,null));
        deserts.add(new Desert("库布其沙漠",10,null));
        deserts.add(new Desert("塔克拉玛干沙漠",11,null));

    }


}
